package com.example.sys.controller;


import com.alibaba.fastjson.JSON;
import com.example.common.resp.Response;
import com.example.sys.bean.UserCreateForm;
import com.example.sys.bean.UserLoginForm;
import com.example.sys.entity.User;
import com.example.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhengDing
 * @since 2022-02-19 22:26:17
 */
@Controller
@Slf4j
@RequestMapping("/sys/user")
@CrossOrigin("*")
@Api(tags = "用户登录")
public class UserController {
    
    @Resource
    private UserService userService;
    
    @RequestMapping("login1")
    @ResponseBody
    @ApiOperation("登录1")
    public Response login1(@RequestBody UserLoginForm form, HttpServletResponse response) {
        String token = userService.login(form.getUsername(), form.getPassword());
        if (StringUtils.hasLength(token)) {
            HashMap<String, Object> payload = new HashMap<>();
            payload.put("token", token);
            return Response.buildSuccess(payload);
        }
        
        return Response.buildError("用户名或密码错误");
    }
    
    @ApiOperation("登录")
    @RequestMapping("login")
    public void login(@RequestBody UserLoginForm form, HttpServletResponse response) throws IOException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        //response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        Response res = null;
        
        String token = userService.login(form.getUsername(), form.getPassword());
        if (StringUtils.hasLength(token)) {
            HashMap<String, Object> payload = new HashMap<>();
            payload.put("token", token);
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(20);
            //cookie.setComment("this is comment");
            //cookie.setDomain("localhost");
            response.addCookie(cookie);
            res = Response.buildSuccess(0, payload, "OK");
            writer.print(JSON.toJSONString(res));
            return;
        }
    
        res = Response.buildError("用户名或密码错误");
        writer.print(JSON.toJSONString(res));
    
        //return Response.buildError("用户名或密码错误");
    }
    
    @RequestMapping("list")
    @ResponseBody
    @ApiOperation("用户列表")
    public Response list(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        System.out.println(session.getMaxInactiveInterval());
    
        String token = request.getHeader("token");
        log.info("token: ", token);
    
        //@CookieValue("token") String token
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                log.info(cookie.getName() + ": " + cookie.getValue());
            }
        }
        
        //log.info("token： ", token);
        return Response.buildSuccess(userService.list());
    }
    
    @PostMapping("save")
    @ResponseBody
    @ApiOperation("新建用户")
    public Response save(@RequestBody UserCreateForm form) {
        if (userService.isUserNameExist(form.getUsername())) {
            return Response.buildError("用户名已存在");
        }
        userService.save(new User(form));
        return Response.buildSuccess();
    }
}
